package data;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.imageio.ImageIO;

import modele.Serie;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class SerieConverter implements Converter {
	
	Bd database;
	
	public SerieConverter(Bd database) {
		this.database = database;
	}
	
    @SuppressWarnings("rawtypes")
	@Override
    public boolean canConvert(Class clazz) {
        return clazz.equals(Serie.class);
    }
    
    @Override
    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
        Serie serie = (Serie) value;
        
        // ID
        writer.startNode("id");
        writer.setValue(serie.getId().toString());
        writer.endNode();
        
        // Nom
        writer.startNode("nom");
        writer.setValue(serie.getNom());
        writer.endNode();
        
        // URL
        writer.startNode("url");
        writer.setValue(serie.getUrl());
        writer.endNode();
        
        // Titre
        writer.startNode("titre");
        writer.setValue(serie.titre);
        writer.endNode();
        
        // titre original
        writer.startNode("titre_original");
        writer.setValue(serie.titre_original);
        writer.endNode();
        
        // Auteur
        writer.startNode("auteur");
        writer.setValue(serie.auteur.getId().toString());
        writer.endNode();
        
        // Dessinateur
        writer.startNode("dessinateur");
        writer.setValue(serie.dessinateur.getId().toString());
        writer.endNode();
        
        // Type
        writer.startNode("type");
        writer.setValue(serie.type.getId().toString());
        writer.endNode();
        
        // Genre
        writer.startNode("genre");
        writer.setValue(serie.genre.getId().toString());
        writer.endNode();
        
        // Édition
        writer.startNode("edition");
        writer.setValue(serie.edition.getId().toString());
        writer.endNode();
        
        // Pays
        writer.startNode("pays");
        writer.setValue(serie.pays.getId().toString());
        writer.endNode();
        
        // Nb volumes
        writer.startNode("nb_volumes");
        writer.setValue(Integer.toString(serie.nb_volumes));
        writer.endNode();
        
        // Nb volumes orignals
        writer.startNode("nb_volumes_o");
        writer.setValue(Integer.toString(serie.nb_volumes_original));
        writer.endNode();
        
        // Age conseillé
        writer.startNode("age_conseille");
        writer.setValue(Integer.toString(serie.age_conseille));
        writer.endNode();
        
        // Image
        // On ne sauvegarde pas l'image, on va juste la mettre dans un fichier
        if (serie.imageFile != null) {
        	try {
	            File outputfile = new File(database.ROOT_PATH + database.IMAGES_PATH + serie.getId().toString() + ".png");
	            BufferedImage bufferedImage = new BufferedImage(serie.image.getWidth(null), serie.image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
	            Graphics2D g = bufferedImage.createGraphics();
	            g.drawImage(serie.image, null, null);
	            ImageIO.write(bufferedImage, "png", outputfile);
	        	serie.imageFile = null;
	        }
        	catch (IOException e) {
        		// keep old image, lol
        	}
	    }
    }
    
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        // ID
        reader.moveDown();
        String id = reader.getValue();
        reader.moveUp();
        
        Serie serie = new Serie(UUID.fromString(id));
        
        // Nom
        reader.moveDown();
        serie.setNom(reader.getValue());
        reader.moveUp();
        
        // URL
        reader.moveDown();
        serie.setUrl(reader.getValue());
        reader.moveUp();
        
        // Titre
        reader.moveDown();
        serie.titre = reader.getValue();
        reader.moveUp();
        
        // Titre original
        reader.moveDown();
        serie.titre_original = reader.getValue();
        reader.moveUp();
        
        // Auteur
        reader.moveDown();
        serie.auteur = this.database.auteurs.get(UUID.fromString(reader.getValue()));
        reader.moveUp();
        
        // Dessinateur
        reader.moveDown();
        serie.dessinateur = this.database.auteurs.get(UUID.fromString(reader.getValue()));
        reader.moveUp();
        
        // Type
        reader.moveDown();
        serie.type = this.database.types.get(UUID.fromString(reader.getValue()));
        reader.moveUp();
        
        // Genre
        reader.moveDown();
        serie.genre = this.database.genres.get(UUID.fromString(reader.getValue()));
        reader.moveUp();
        
        // Édition
        reader.moveDown();
        serie.edition = this.database.editeurs.get(UUID.fromString(reader.getValue()));
        reader.moveUp();
        
        // Pays
        reader.moveDown();
        serie.pays = this.database.pays.get(UUID.fromString(reader.getValue()));
        reader.moveUp();
        
        // Nb volumes
        reader.moveDown();
        try {
        	serie.nb_volumes = Integer.parseInt(reader.getValue());
        }
        catch (NumberFormatException e) {
        	serie.nb_volumes = 0;
        }
        reader.moveUp();
        
        // Nb volumes original
        reader.moveDown();
        try {
        	serie.nb_volumes_original = Integer.parseInt(reader.getValue());
        }
        catch (NumberFormatException e) {
        	serie.nb_volumes_original = 0;
        }
        reader.moveUp();
        
        // Age conseillé
        reader.moveDown();
        try {
        	serie.age_conseille = Integer.parseInt(reader.getValue());
        }
        catch (NumberFormatException e) {
        	serie.age_conseille = 0;
        }
        reader.moveUp();
        
        // Image
        File inputFile = new File(database.ROOT_PATH + database.IMAGES_PATH + serie.getId().toString() + ".png");
		try {
			serie.image = ImageIO.read(inputFile);
		} catch (IOException e) {
			// No image... Doh !
		}
        
        return serie;
    }
}
